import './index.css'
import {_User} from '@/service/user-service.js'
import {trimStr} from '@/util/dom/index.js'

let _ErrMsg = {
	show : function(errTip){
		
		let errItem = document.getElementsByClassName('error-item')[0]
		errItem.setAttribute("style","display:block");
		let errMsg =  errItem.getElementsByClassName('err-msg')[0]
		if(errTip){
			errMsg.innerHTML = errTip;
		}
		
	},
	hide : function(){
		console.log("run")
		document.getElementsByClassName('error-item')[0].setAttribute("style","display:none");
	}
}

var _Login = {
	init : function(){
		_ErrMsg.hide()
		this.bindEvent()
	},
	bindEvent : function(){
		document.getElementById('submit').addEventListener('click',() => {
			this.submit()
		})
	},
	//login submit
	submit : function(){
		let data = {
			username : trimStr(document.getElementById('username').value),
			password : trimStr(document.getElementById('password').value)
		}
		let result  = this.validation(data)
		if(result.status === true){
			_User.getUserInfo(data,function(data,msg){
				console.log(data)
			},function(){
				console.log('login error')
			})
		}else{
			_ErrMsg.show(result.msg)
		}
	},
	validation : function(param){
		let result = {
			msg : '',
			status : false
		}
		if(param.username === ""){
			result.msg = `username can't  null`
			
			return result
		}
		if(param.password === ''){
			result.msg = `password can't be null`
			
			return result
		}
		result.status = true
		return result

	},

};


(() =>{
	_Login.init()
})()